#include "NewDummy.h"
#include "setBehavior.h"
#include "Behavior/AllBehavior.h"
#include "Behavior/setSeek.h"

#include "AnimationPikmenDown.h"
#include "AnimationPikmenUp.h"
#include "AnimationPikmenRight.h"
#include "AnimationPikmenLeft.h"

#include <ctime>
#include <stdlib.h>

#include <SDL/SDL_gfxPrimitives.h>

bool NewDummy::OnInit(Character* target){
return true;
}

/*
 * Inicializa al bicho
 */
bool NewDummy::OnInit(vector<Character*>* ch,vector<Obstacle*>* ob,int posX, int posY,Character* target){
//Behavior *beh;

  if((surface = CSurface::OnLoad("images/dummy.png",true)) == NULL){
    cout << "Algo salio mal" << endl;
    return false;
  }
  if((carita = CSurface::OnLoad("images/dummy_head.png",true)) == NULL){
    cout << "Algo salio mal" << endl;
    return false;
  }

  // animaciones...
  animations = new AnimationPikmenLeft();

  /* Posicionamos el personaje */
  k = new Kinematic();
  k->position[0] = posX;
  k->position[1] = posY;
//k->position[0] = 100;
//k->position[1] = 100;
  k->orientation = M_PI*((float) (random()%7))/7;
  //Estos valores de max_* es para Wander
//k->max_velocity = 0.05;
//k->max_rotation = 0.001;

  //Estos valores de max_* es para Arrive 
  k->max_velocity = 0.035;
  k->max_rotation = 0.002;
  this->width = this->height = 20;
  
  behaviors.push_back(new setSeek(this,target,ch,ob));
  behaviors.push_back(new Waiting(this,ch,ob));
//cout << "Salio del OnInit de NewDummy.cpp" << endl;
  return true;
}


/*
 * Pinta al bicho
 */
void NewDummy::OnRender(SDL_Surface* display){
  CSurface::OnDraw(display, this->surface,
      this->k->position[0],
      this->k->position[1]);//,0,0,40,68);
  CSurface::OnDraw(display, this->carita,
      15*sin(k->orientation) +k->position[0]+ width/2+2,
      15*cos(k->orientation) +k->position[1]+ height/2+2);

  // Los frames hay que tomarlos de las animaciones
  CSurface::OnDraw(display, animations->getFrame(),
      this->k->position[0],
      this->k->position[1],
      animations->getX(), animations->getY(), 
      animations->getWidth(), animations->getHeight()
      );

  // Probando libreria de primitivas
  filledCircleRGBA(display,
      this->k->position[0], this->k->position[1], 20,
      0, 0, 0, 128
      );

  // printf("pos x = %f , pos y = %f \n",k->position[0]+ (this->width/2),k->position[0]+ (this->height/2));
}

/*
* Aplica comportamiento, actualiza la posicion
* y anima sprites (proximamente)
*/
void NewDummy::OnLoop(long time){
  Steering* steering = this->getSteering();
// DEBUG
// cout << "S-Ang " << steering->angular << endl;
// cout << "S-Lin " << steering->linear[0]<< "," << steering->linear[1] << endl;
//cout << "rotation  " << k->orientation << endl;
//cout << "orientacion " << k->rotation << endl;
  update(steering, time);
  delete steering;

  // Hay que animarlo
  animations->OnAnimate();
}

void NewDummy::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode){
  Kinematic* current = this->getKinematic();
  switch(sym){
    case SDLK_UP:
      cout << "up" << endl;
      current->position[1] -= 10;
      this->setKinematic(current);
      break;
    case SDLK_DOWN:
      cout << "down" << endl;
      current->position[1] += 10;
      this->setKinematic(current);
      break;
    case SDLK_RIGHT:
      cout << "right " << current->position[0];
      current->position[0] += 10;
      this->setKinematic(current);
      cout << " => " << current->position[0] << endl;
      break;
    case SDLK_LEFT:
      cout << "left" << endl;
      current->position[0] -= 10;
      this->setKinematic(current);
      break;
    case SDLK_m:
      cout << "lo lograste " << endl;
    default:
      ;
  }
  delete(current);
}

void NewDummy::OnWheelButtonUp(int mX, int mY){
  Kinematic* current = this->getKinematic();
  current->orientation += M_PI / 16.0f;
  this->setKinematic(current);
}

void NewDummy::OnWheelButtonDown(int mX, int mY){
  Kinematic* current = this->getKinematic();
  current->orientation -= M_PI / 16.0f;
  this->setKinematic(current);
}

void NewDummy::OnMButtonDown(int mX, int mY){
  Kinematic* current = this->getKinematic();
  current->position[0] = mX - width/2;
  current->position[1] = mY - height/2;
  this->setKinematic(current);
}

void NewDummy::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle){
  if(Middle) OnMButtonDown(mX, mY);
}

